<template>
	<view>
		<block v-if="isload">
			<view class="swiper-container">
				<swiper class="swiper" :indicator-dots="false" :autoplay="true" :interval="5000" @change="swiperChange">
					<block v-for="(item, index) in product.pics" :key="index">
						<swiper-item class="swiper-item">
							<view class="swiper-item-view">
								<image class="img" :src="item" mode="widthFix" />
							</view>
						</swiper-item>
					</block>
				</swiper>
				<view class="imageCount">{{ current + 1 }}/{{ (product.pics).length }}</view>
			</view>
			<view class="header">
				<view class="price_share">
					<view class="price">
						<view class="f1" :style="{ color: t('color1') }">{{ product.score_price }}{{ t('积分') }}<text
								v-if="product.money_price * 1 > 0">+{{ product.money_price }}元</text></view>
						<view class="f2">￥{{ product.sell_price }} </view>
					</view>
					<!-- <view class="share" @tap="shareClick"><image class="img" src="/static/img/share.png"/><text class="txt">分享</text></view> -->
				</view>
				<view class="title">{{ product.name }}</view>
				<view class="sales_stock">
					<view class="f1">已购买{{ product.sales }}件</view>
					<view class="f2">库存：{{ product.stock }}</view>
				</view>
				<view class="commission" :style="{ background: 'rgba(' + t('color1rgb') + ',0.1)', color: t('color1') }"
					v-if="shopset.showcommission == 1 && (product.commission > 0 || product.commission_score > 0)">
					分享好友购买预计可得{{ t('佣金') }}：
					<block v-if="product.commission > 0"><text style="font-weight:bold;padding:0 2px">{{
						product.commission }}</text>{{ product.commission_desc }}
					</block>
					<text v-if="product.commission > 0 && product.commission_score > 0">+</text>
					<block v-if="product.commission_score > 0"><text style="font-weight:bold;padding:0 2px">{{
						product.commission_score }}</text>{{ product.commission_score_desc }}
					</block>
				</view>
			</view>

			<view class="shop" v-if="shopset.showjd == 1">
				<image :src="business.logo" class="p1" />
				<view class="p2 flex1">
					<view class="t1">{{ business.name }}</view>
					<view class="t2">{{ business.desc }}</view>
				</view>
				<button class="p4"
					:style="{ background: 'linear-gradient(90deg,' + t('color1') + ' 0%,rgba(' + t('color1rgb') + ',0.8) 100%)' }"
					@tap="goto"
					:data-url="product.bid == 0 ? '/pages/index/index' : '/pagesExt/business/index?id=' + product.bid"
					data-opentype="reLaunch">进入店铺</button>
			</view>
			<view class="detail_title">
				<view class="t1"></view>
				<view class="t2"></view>
				<view class="t0">商品描述</view>
				<view class="t2"></view>
				<view class="t1"></view>
			</view>
			<view class="detail">
				<dp :pagecontent="pagecontent"></dp>
			</view>

			<view style="width:100%;height:140rpx;"></view>
			<view class="bottombar flex-row" :class="menuindex > -1 ? 'tabbarbot' : 'notabbarbot'"
				v-if="product.status == 1">
				<view class="f1">
					<!-- <view class="item" @tap="goto" :data-url="kfurl" v-if="kfurl!='contact::'">
					<image class="img" src="/static/img/kefu.png"/>
					<view class="t1">客服</view>
				</view> -->
					<button class="item" open-type="contact">
						<image class="img" src="/static/img/kefu.png" />
						<view class="t1">客服</view>
					</button>
					<view class="item flex1" @tap="goto" :data-url="'cart?type=1'">
						<image class="img" src="/static/img/gwc.png" />
						<view class="t1">购物车</view>
						<view class="cartnum" v-if="cartnum > 0">{{ cartnum }}</view>
					</view>
					<view class="item" @tap="addfavorite">
						<image class="img"
							:src="isfavorite ? static_url + '/static/img/shoucanged.png' : static_url + '/static/img/shoucang.png'" />
						<view class="t1">{{ isfavorite ? '已收藏' : '收藏' }}</view>
					</view>
				</view>
				<view class="op">
					<view class="tocart flex-x-center flex-y-center" @tap="addcart" :disabled="product.stock === 0"
						:class="{ 'disabled': product.stock === 0 }" :style="{ background: t('color2') }">加入购物车</view>
					<block v-if="opt.type == 1">
						<!-- <view class="tobuy flex-x-center flex-y-center" @tap="tobuy" :style="{background:t('color1')}">立即购买
						</view> -->
						<view class="tobuy flex-x-center flex-y-center" @tap="open"
							:style="{ background: t('color1') }">
							立即购买
						</view>
					</block>
					<block v-else>
						<view class="tobuy flex-x-center flex-y-center" @tap="open"
							:style="{ background: t('color1') }">
							立即兑换
						</view>
					</block>
				</view>
			</view>
			<uni-popup ref="notice" class="pop" type="bottom">
				<view class="popup-box">
					<view class="close" @tap="buydialogChange">
						<image src="/static/img/close.png" class="image" />
					</view>
					<view class="buynum flex flex-y-center">
						<view class="flex1">购买数量：</view>
						<view class="addnum">
							<view class="minus" @tap="gwcminus">
								<image class="img" src="/static/img/cart-minus.png" />
							</view>
							<input class="input" type="number" :value="gwcnum" @input="gwcinput"></input>
							<view class="plus" @tap="gwcplus">
								<image class="img" src="/static/img/cart-plus.png" />
							</view>
						</view>
					</view>
					<view class="op1">

						<!-- <block
							>
							<button class="nostock">库存不足</button>
						</block> -->
						<block>

							<button class="addcart" :style="{ backgroundColor: t('color2') }" @tap="tobuy">确 定</button>

						</block>
					</view>
				</view>

			</uni-popup>
			<scrolltop :isshow="scrolltopshow"></scrolltop>

			<view v-if="sharetypevisible" class="popup__container">
				<view class="popup__overlay" @tap.stop="handleClickMask"></view>
				<view class="popup__modal" style="height:320rpx;min-height:320rpx">
					<!-- <view class="popup__title">
					<text class="popup__title-text">请选择分享方式</text>
					<image src="/static/img/close.png" class="popup__close" style="width:36rpx;height:36rpx" @tap.stop="hidePstimeDialog"/>
				</view> -->
					<view class="popup__content">
						<view class="sharetypecontent">
							<view class="f1" @tap="shareapp" v-if="getplatform() == 'app'">
								<image class="img" src="/static/img/weixin.png" />
								<text class="t1">分享给好友</text>
							</view>
							<view class="f1" @tap="sharemp" v-else-if="getplatform() == 'mp'">
								<image class="img" src="/static/img/weixin.png" />
								<text class="t1">分享给好友</text>
							</view>
							<view class="f1" @tap="sharemp" v-else-if="getplatform() == 'h5'">
								<image class="img" src="/static/img/weixin.png" />
								<text class="t1">分享给好友</text>
							</view>
							<button class="f1" open-type="share" v-else>
								<image class="img" src="/static/img/weixin.png" />
								<text class="t1">分享给好友</text>
							</button>
							<view class="f2" @tap="showPoster">
								<image class="img" :src="static_url + '/static/img/sharepic.png'" />
								<text class="t1">生成分享图片</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="posterDialog" v-if="showposter">
				<view class="main">
					<view class="close" @tap="posterDialogClose">
						<image class="img" src="/static/img/close.png" />
					</view>
					<view class="content">
						<image class="img" :src="posterpic" mode="widthFix" @tap="previewImage" :data-url="posterpic">
						</image>
					</view>
				</view>
			</view>

		</block>
		<loading v-if="loading"></loading>
		<dp-tabbar :opt="opt" @getmenuindex="getmenuindex"></dp-tabbar>
		<popmsg ref="popmsg"></popmsg>
	</view>
</template>

<script>
var app = getApp();
export default {
	data() {
		return {
			opt: {},
			loading: false,
			isload: false,
			menuindex: -1,

			isfavorite: false,
			current: 0,
			sysset: {},
			business: {},
			product: [],
			cartnum: "",
			pagecontent: "",
			shopset: "",
			title: "",
			sharepic: "",
			sharetypevisible: false,
			showposter: false,
			posterpic: "",
			scrolltopshow: false,
			kfurl: '',
			isButtonDisabled: false,
			gwcnum: 1,
			pre_url: app.globalData.pre_url, static_url: app.globalData.static_url,
		}
	},
	onLoad: function (opt) {
		this.opt = app.getopts(opt);
		this.getdata();
	},
	onPullDownRefresh: function () {
		this.getdata();
	},
	onShareAppMessage: function () {
		return this._sharewx({
			title: this.product.name,
			pic: this.product.pic
		});
	},
	onShareTimeline: function () {
		var sharewxdata = this._sharewx({
			title: this.product.name,
			pic: this.product.pic
		});
		var query = (sharewxdata.path).split('?')[1] + '&seetype=circle';
		return {
			title: sharewxdata.title,
			imageUrl: sharewxdata.imageUrl,
			query: query
		}
	},
	methods: {
		open() {
			this.$refs.notice.open()
		},
		buydialogChange() {
			this.$refs.notice.close()
		},
		//加
		gwcplus: function (e) {
			var gwcnum = this.gwcnum + 1;
			var ks = this.ks;
			if (gwcnum > this.product.stock) {
				app.error('库存不足');
				return 1;
			}

			this.gwcnum = this.gwcnum + 1;
		},
		//减
		gwcminus: function (e) {
			if (this.gwcnum <= 1) {
				app.error('数量不能再减少了');
				return;
			}

			var gwcnum = this.gwcnum - 1;
			this.gwcnum = gwcnum;
		},
		//输入
		gwcinput: function (e) {
			var inputValue = parseInt(e.target.value); // 获取输入框的值，并转换为整数

			// 如果输入的值不是数字或者小于等于0，或者大于库存数量，提示错误并重置为1
			if (isNaN(inputValue) || inputValue <= 0) {
				app.error('数量不能小于1');
				this.gwcnum = 1; // 重置为1，或者你可以根据需求设置其他默认值
				return;
			}

			if (inputValue > this.product.stock) {
				app.error('库存不足');
				this.gwcnum = this.product.stock; // 重置为库存数量
				return;
			}

			// 如果输入的值有效，更新购物车数量
			this.gwcnum = inputValue;
		},
		getdata: function () {
			var that = this;
			var id = this.opt.id || 0;
			that.loading = true;
			app.post('ApiScoreshop/product', {
				id: id
			}, function (res) {
				that.loading = false;
				if (res.status == 0) {
					app.alert(res.msg);
					return;
				}
				var product = res.product;
				var pagecontent = JSON.parse(product.detail);
				that.sysset = res.sysset;
				that.product = product;
				that.cartnum = res.cartnum;
				that.pagecontent = pagecontent;
				if(res.shopset){
					that.shopset =res.shopset;
				}
				that.business = res.business;
				that.title = product.name;
				that.isfavorite = res.isfavorite;
				that.sharepic = product.pics[0];
				uni.setNavigationBarTitle({
					title: product.name
				});
				that.kfurl = '/pages/kefu/index';
				if (app.globalData.initdata.kfurl != '') {
					that.kfurl = app.globalData.initdata.kfurl;
				}
				that.loaded({
					title: res.product.name,
					pic: res.product.pic
				});
			});
		},
		swiperChange: function (e) {
			var that = this;
			that.current = e.detail.current
		},
		addcart: function () {
			var that = this;
			var proid = that.product.id;
			that.loading = true;
			app.post('ApiScoreshop/addcart', {
				proid: proid,
				num: 1
			}, function (res) {
				that.loading = false;
				if (res.status == 1) {
					app.success(res.msg);
					that.cartnum++;
				} else {
					app.error(res.msg);
				}
			});
		},
		tobuy: function () {
			var proid = this.product.id;
			var prodata = proid + ',' + this.gwcnum;
			app.goto('buy?prodata=' + prodata);
			this.$refs.notice.close()
			this.gwcnum = 1
		},
		//收藏操作
		addfavorite: function () {
			var that = this;
			var proid = that.product.id;
			app.post('ApiScoreshop/addfavorite', {
				proid: proid,
				type: 'scoreshop'
			}, function (data) {
				if (data.status == 1) {
					that.isfavorite = !that.isfavorite;
				}
				app.success(data.msg);
			});
		},
		onPageScroll: function (e) {
			var that = this;
			var scrollY = e.scrollTop;
			if (scrollY > 200) {
				that.scrolltopshow = true;
			}
			if (scrollY < 150) {
				that.scrolltopshow = false
			}
		},
		shareClick: function () {
			this.sharetypevisible = true;
		},
		handleClickMask: function () {
			this.sharetypevisible = false
		},
		showPoster: function () {
			var that = this;
			that.showposter = true;
			that.sharetypevisible = false;
			app.showLoading('生成海报中');

			app.post('ApiScoreshop/getposter', {
				proid: that.product.id,
				type: 'score'
			}, function (data) {
				app.showLoading(false);
				if (data.status == 0) {
					app.alert(data.msg);
				} else {
					that.posterpic = data.poster;
				}
			});
		},
		posterDialogClose: function () {
			this.showposter = false;
		},
		showcuxiaodetail: function () {
			this.showcuxiaodialog = true;
		},
		hidecuxiaodetail: function () {
			this.showcuxiaodialog = false
		},
		getcoupon: function () {
			this.showcuxiaodialog = false;
			this.getdata();
		},
		onPageScroll: function (e) {
			var that = this;
			var scrollY = e.scrollTop;
			if (scrollY > 200) {
				that.scrolltopshow = true;
			}
			if (scrollY < 150) {
				that.scrolltopshow = false
			}
		},
		sharemp: function () {
			app.error('点击右上角发送给好友或分享到朋友圈');
			this.sharetypevisible = false
		},
		shareapp: function () {
			var that = this;
			that.sharetypevisible = false;
			uni.showActionSheet({
				itemList: ['发送给微信好友', '分享到微信朋友圈'],
				success: function (res) {
					if (res.tapIndex >= 0) {
						var scene = 'WXSceneSession';
						if (res.tapIndex == 1) {
							scene = 'WXSenceTimeline';
						}
						var sharedata = {};
						sharedata.provider = 'weixin';
						sharedata.type = 0;
						sharedata.scene = scene;
						sharedata.title = that.product.name;
						//sharedata.summary = app.globalData.initdata.desc;
						sharedata.href = app.globalData.pre_url + '/h5/' + app.globalData.aid +
							'.html#/activity/scoreshop/product?scene=id_' + that.product.id + '-pid_' +
							app.globalData.mid;
						sharedata.imageUrl = that.product.pic;
						var sharelist = app.globalData.initdata.sharelist;
						if (sharelist) {
							for (var i = 0; i < sharelist.length; i++) {
								if (sharelist[i]['indexurl'] == '/activity/scoreshop/product') {
									sharedata.title = sharelist[i].title;
									sharedata.summary = sharelist[i].desc;
									sharedata.imageUrl = sharelist[i].pic;
									if (sharelist[i].url) {
										var sharelink = sharelist[i].url;
										if (sharelink.indexOf('/') === 0) {
											sharelink = app.globalData.pre_url + '/h5/' + app
												.globalData.aid + '.html#' + sharelink;
										}
										if (app.globalData.mid > 0) {
											sharelink += (sharelink.indexOf('?') === -1 ? '?' : '&') +
												'pid=' + app.globalData.mid;
										}
										sharedata.href = sharelink;
									}
								}
							}
						}
						uni.share(sharedata);
					}
				}
			});
		}
	}
}
</script>
<style>
.swiper-container {
	position: relative
}

.swiper {
	width: 100%;
	height: 750rpx;
	overflow: hidden;
}

.swiper-item-view {
	width: 100%;
	height: 750rpx;
}

.swiper .img {
	width: 100%;
	height: 750rpx;
	overflow: hidden;
}

.imageCount {
	width: 100rpx;
	height: 50rpx;
	background-color: rgba(0, 0, 0, 0.3);
	border-radius: 40rpx;
	line-height: 50rpx;
	color: #fff;
	text-align: center;
	font-size: 26rpx;
	position: absolute;
	right: 13px;
	bottom: 20rpx;
}

.provideo {
	background: rgba(255, 255, 255, 0.7);
	width: 160rpx;
	height: 54rpx;
	padding: 0 20rpx 0 4rpx;
	border-radius: 27rpx;
	position: absolute;
	bottom: 30rpx;
	left: 50%;
	margin-left: -80rpx;
	display: flex;
	align-items: center;
	justify-content: space-between
}

.provideo image {
	width: 50rpx;
	height: 50rpx;
}

.provideo .txt {
	flex: 1;
	text-align: center;
	padding-left: 10rpx;
	font-size: 24rpx;
	color: #333
}

.videobox {
	width: 100%;
	height: 750rpx;
	text-align: center;
	background: #000
}

.videobox .video {
	width: 100%;
	height: 650rpx;
}

.videobox .parsevideo {
	margin: 0 auto;
	margin-top: 20rpx;
	height: 40rpx;
	line-height: 40rpx;
	color: #333;
	background: #ccc;
	width: 140rpx;
	border-radius: 25rpx;
	font-size: 24rpx
}

.header {
	width: 100%;
	padding: 20rpx 3%;
	background: #fff;
}

.header .price_share {
	width: 100%;
	height: 100rpx;
	display: flex;
	align-items: center;
	justify-content: space-between
}

.header .price_share .price {
	display: flex;
	align-items: flex-end
}

.header .price_share .price .f1 {
	font-size: 36rpx;
	color: #51B539;
	font-weight: bold
}

.header .price_share .price .f2 {
	font-size: 26rpx;
	color: #C2C2C2;
	text-decoration: line-through;
	margin-left: 30rpx;
	padding-bottom: 2px
}

.header .price_share .share {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center
}

.header .price_share .share .img {
	width: 32rpx;
	height: 32rpx;
	margin-bottom: 2px
}

.header .price_share .share .txt {
	color: #333333;
	font-size: 20rpx
}

.header .title {
	color: #000000;
	font-size: 32rpx;
	line-height: 42rpx;
	font-weight: bold;
}

.header .sales_stock {
	display: flex;
	justify-content: space-between;
	height: 60rpx;
	line-height: 60rpx;
	margin-top: 30rpx;
	font-size: 24rpx;
	color: #777777
}

.header .commission {
	display: inline-block;
	margin-top: 20rpx;
	margin-bottom: 10rpx;
	border-radius: 10rpx;
	font-size: 20rpx;
	height: 44rpx;
	line-height: 44rpx;
	padding: 0 20rpx
}

.shop {
	display: flex;
	align-items: center;
	width: 100%;
	background: #fff;
	margin-top: 20rpx;
	padding: 20rpx 3%;
	position: relative;
	min-height: 100rpx;
}

.shop .p1 {
	width: 90rpx;
	height: 90rpx;
	border-radius: 6rpx;
	flex-shrink: 0
}

.shop .p2 {
	padding-left: 10rpx
}

.shop .p2 .t1 {
	width: 100%;
	height: 40rpx;
	line-height: 40rpx;
	overflow: hidden;
	color: #111;
	font-weight: bold;
	font-size: 30rpx;
}

.shop .p2 .t2 {
	width: 100%;
	height: 30rpx;
	line-height: 30rpx;
	overflow: hidden;
	color: #999;
	font-size: 24rpx;
	margin-top: 8rpx
}

.shop .p4 {
	height: 64rpx;
	line-height: 64rpx;
	color: #FFFFFF;
	border-radius: 32rpx;
	margin-left: 20rpx;
	flex-shrink: 0;
	padding: 0 30rpx;
	font-size: 24rpx;
	font-weight: bold
}

.detail {
	min-height: 200rpx;
}

.detail_title {
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 60rpx;
	margin-bottom: 30rpx
}

.detail_title .t0 {
	font-size: 28rpx;
	font-weight: bold;
	color: #222222;
	margin: 0 20rpx
}

.detail_title .t1 {
	width: 12rpx;
	height: 12rpx;
	background: rgba(253, 74, 70, 0.2);
	transform: rotate(45deg);
	margin: 0 4rpx;
	margin-top: 6rpx
}

.detail_title .t2 {
	width: 18rpx;
	height: 18rpx;
	background: rgba(253, 74, 70, 0.4);
	transform: rotate(45deg);
	margin: 0 4rpx
}

.bottombar {
	width: 94%;
	position: fixed;
	bottom: 0px;
	left: 0px;
	background: #fff;
	display: flex;
	height: 100rpx;
	padding: 0 4% 0 2%;
	align-items: center;
	box-sizing: content-box
}

.bottombar .f1 {
	flex: 1;
	display: flex;
	align-items: center;
	margin-right: 30rpx
}

.bottombar .f1 .item {
	display: flex;
	flex-direction: column;
	align-items: center;
	width: 80rpx;
	position: relative
}

.bottombar .f1 .item .img {
	width: 44rpx;
	height: 44rpx
}

.bottombar .f1 .item .t1 {
	font-size: 18rpx;
	color: #222222;
	height: 30rpx;
	line-height: 30rpx;
	margin-top: 6rpx
}

.bottombar .op {
	width: 60%;
	border-radius: 36rpx;
	overflow: hidden;
	display: flex;
}

.bottombar .tocart {
	flex: 1;
	height: 72rpx;
	line-height: 72rpx;
	color: #fff;
	border-radius: 0px;
	border: none;
	font-size: 28rpx;
	font-weight: bold
}

.bottombar .tobuy {
	flex: 1;
	height: 72rpx;
	line-height: 72rpx;
	color: #fff;
	border-radius: 0px;
	border: none;
}

.bottombar .cartnum {
	position: absolute;
	right: 4rpx;
	top: -4rpx;
	background: rgba(253, 74, 70, 0.8);
	color: #fff;
	border-radius: 50%;
	width: 32rpx;
	height: 32rpx;
	line-height: 32rpx;
	text-align: center;
	font-size: 22rpx;
}

.disabled {
	pointer-events: none;
	/* 禁止点击 */
	background-color: #ccc;
	/* 灰色背景 */
}

.buynum {
	width: 94%;
	position: relative;
	margin: 0 3%;
	padding: 40rpx 0px 10px 0px;
}

.addnum {
	font-size: 30rpx;
	color: #666;
	width: auto;
	display: flex;
	align-items: center
}

.addnum .plus {
	width: 65rpx;
	height: 48rpx;
	background: #F6F8F7;
	display: flex;
	align-items: center;
	justify-content: center
}

.addnum .minus {
	width: 65rpx;
	height: 48rpx;
	background: #F6F8F7;
	display: flex;
	align-items: center;
	justify-content: center
}

.addnum .img {
	width: 24rpx;
	height: 24rpx
}

.addnum .input {
	flex: 1;
	width: 50rpx;
	border: 0;
	text-align: center;
	color: #2B2B2B;
	font-size: 28rpx;
	margin: 0 15rpx;
}

.uni-popup__wrapper-box {
	background-color: #fff;
}

.popup-box {
	background-color: #fff;
	border-radius: 20rpx 20rpx 0 0;
	position: relative;
	padding: 40rpx 0;
}

.op1 {
	border-radius: 36rpx;
	overflow: hidden;
	display: flex;
	margin-top: 40rpx;
	margin-left: 5%;
	margin-right: 5%;
}

.addcart {
	flex: 1;
	height: 72rpx;
	line-height: 72rpx;
	color: #fff;
	border-radius: 0px;
	border: none;
	font-size: 28rpx;
	font-weight: bold
}

.close {
	position: absolute;
	top: 0;
	right: 0;
	padding: 20rpx;
	z-index: 12
}

.close .image {
	width: 30rpx;
	height: 30rpx;
}
</style>